<?php
$bartUserServices = new BartuserService();
$customerServices = new CustomerServices();
$orderServices = new OrdersService();
$orderDetailService = new OrderDetailsService();
if(isset($_GET['opt']))
{
	$opt = $_GET['opt'];
	$moduleID = $moduleService->getModuleIDByFuncAndOpt($context, $result, $func, $opt);
	$permit = $permitService->checkPermition($context, $result, $_SESSION['user']['type'], $moduleID);
	if($opt == "add" && $permit ==true)
	{
		$accountService = new AccountService();
		$remarks = $accountService->getRemarks($context, $result, 1);
		$smarty->assign('remarks', $remarks);
		$orderDetailArr = array();
		if(isset($_SESSION['orderDetail']))
		{
			foreach ($_SESSION['orderDetail'] as $orderDetail)
			{
				$orderDetailArr[] = unserialize($orderDetail);
			}

			$smarty->assign('orderDetailArr', $orderDetailArr);
		}
		if(isset($_SESSION['comm']))
		{
			$smarty->assign('comm', $_SESSION['comm']);
		}
		 
		if(isset($_SESSION['VAT']))
		{
			$smarty->assign('VAT', $_SESSION['VAT']);
		}
		 
		$bArtUser = $bartUserServices->getBArtUserByOid($context, $result, $_SESSION['user']['ID']);
		 
		$smarty->assign('bartUser', $bArtUser);
		$smarty->display($templateDir . "/temp1/addOrder.tpl");
	}
	
	if($opt == "addOrderCus" && $permit ==true)
	{
		$accountService = new AccountService();
		$remarks = $accountService->getRemarks($context, $result, 1);
		$smarty->assign('remarks', $remarks);
		$orderDetailArr = array();
		if(isset($_SESSION['orderDetail']))
		{
			foreach ($_SESSION['orderDetail'] as $orderDetail)
			{
				$orderDetailArr[] = unserialize($orderDetail);
			}

			$smarty->assign('orderDetailArr', $orderDetailArr);
		}
		 
		if(isset($_SESSION['VAT']))
		{
			$smarty->assign('VAT', $_SESSION['VAT']);
		}
		 
		$customer = $customerServices->getCustomerByOid($context, $result, $_SESSION['user']['ID']);
		 
		$smarty->assign('customer', $customer);
		$smarty->display($templateDir . "/temp1/addOrderCus.tpl");
	}

	if($opt == "edit" && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$smarty->display($templateDir . "/temp1/editOrder.tpl");
		}
	}
	if($opt == 'view' && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$order = $orderServices->getOrderByOid($context, $result, $_GET['id']);
			if($order->getOrdersDomain()->getType() == 10 or $order->getOrdersDomain()->getType() == 11)
			{
				$orderDetailArr = $order->getOrderDetail();
				if(!Tool::isEmpty($orderDetailArr))
				{
					$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
				}
				$smarty->assign('order', $order);
				$smarty->display($templateDir . "/temp1/viewOrder.tpl");
			}
			
			if($order->getOrdersDomain()->getType() == 1)
			{
				$orderDetailArr = $order->getOrderDetail();
				if(!Tool::isEmpty($orderDetailArr))
				{
					$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
				}
				$smarty->assign('sellerID', $_SESSION['user']['ID']);
				$smarty->assign('order', $order);
				$smarty->display($templateDir . "/temp1/viewOrder.tpl");
			}
			if($order->getOrdersDomain()->getType() == 3)
			{
				$orderDetailArr = $order->getOrderDetail();
				if(!Tool::isEmpty($orderDetailArr))
				{
					$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
				}
				$smarty->assign('order', $order);
				$smarty->display($templateDir . "/temp1/viewOrderFactPending.tpl");
			}
		}
		 
	}
	if($opt == "list" && $permit ==true)
	{
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countByStatus($context, $result, "1");
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->getAllOrderByTypeByRange($context, $result,"1", $start, $configPage['limit']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listOrder.tpl");
	}
	if($opt == 'searchOrder' && $permit == true)
	{	 
		if(isset($_POST['dateFrom']) && isset($_POST['dateTo']) && $_POST['dateFrom'] != '' && $_POST['dateTo'] != '')
		{
			$dateFrom = $_POST['dateFrom'];
			$dateTo = $_POST['dateTo'];
		}
		else {
			$dateFrom = '0000-00-00';
			$dateTo = date('Y-m-d', time());
		}
		$start=$p->findStart($configPage['limit']);
		$count = $orderServices->countAllByStatusByOrderDate($context, $result, "1", $dateFrom, $dateTo);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->findAllByStatusByOrderDateByRange($context, $result, "1", $start, $configPage['limit'], $dateFrom, $dateTo);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";

		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listOrder.tpl");
	}

	if($opt == 'viewC' && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$order = $orderServices->getOrderByOrderID($context, $result, $_GET['id']);
			if($order->getOrdersDomain()->getType() == 1)
			{	
				$orderDetailArr = $order->getOrderDetail();
				if(!Tool::isEmpty($orderDetailArr))
				{
					$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
				}
				$smarty->assign('order', $order);
				$smarty->display($templateDir . "/temp1/viewOrderC.tpl");
			}
			if($order->getOrdersDomain()->getType() == 2)
			{
				$orderDetailArr = $order->getOrderDetail();
				if(!Tool::isEmpty($orderDetailArr))
				{
					$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
				}
				$smarty->assign('order', $order);
				$smarty->display($templateDir . "/temp1/viewOrderC.tpl");
			}
		}
	}

	if($opt == 'viewCusPending' && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$order = $orderServices->getOrderByOid($context, $result, $_GET['id']);
			$orderDetailArr = $order->getOrderDetail();
			if(!Tool::isEmpty($orderDetailArr))
			{
				$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
			}
			$smarty->assign('order', $order);
			$smarty->display($templateDir . "/temp1/viewOrderCustomerPending.tpl");
		}
	}

	if($opt == "listOderC" && $permit ==true)
	{
		$consigneeID = $_SESSION['user']['ID'];		
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countAllOrderbyConsigneeByStatus($context, $result, $consigneeID, "1,2");
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
			
		$orderCustomerList = $orderServices->getAllOrderByConsigneeByStatus($context, $result, $start, $configPage['limit'], $consigneeID, "1,2");
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderCustomerList', $orderCustomerList);
			
		$smarty->display($templateDir . "/temp1/listOrderCustomer.tpl");
	}

	if($opt == "complain" && $permit ==true)
	{
		$orderID = $_GET['id'];
		$smarty->assign('orderID', $orderID);
			
		$smarty->display($templateDir . "/temp1/complain.tpl");
	}

	if($opt == "searchOrderC")
	{
		if(isset($_POST['dateFrom']) && isset($_POST['dateTo']) && $_POST['dateFrom'] != '' && $_POST['dateTo'] != '')
		{
			$dateFrom = $_POST['dateFrom'];
			$dateTo = $_POST['dateTo'];
		}
		else {
			$dateFrom = '0000-00-00';
			$dateTo = date('Y-m-d', time());
		}
		$consigneeID = $_SESSION['user']['ID'];
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countAllByStatusByConsigneeOrderDate($context, $result, "1,2", $consigneeID, $dateFrom, $dateTo);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);

		$orderCustomerList = $orderServices->findAllByStatusByConsigneeOrderDateByRange($context, $result, "1,2", $start, $configPage['limit'], $consigneeID, $dateFrom, $dateTo);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";

		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderCustomerList', $orderCustomerList);

		$smarty->display($templateDir . "/temp1/listOrderCustomer.tpl");
	}
	if($opt == "listOrderF" && $permit == true)
	{
		$factoryID = $_SESSION['user']['ID'];
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countAllOrderbyFactoryID($context, $result, $factoryID);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
			
		$orderFactoryList = $orderServices->getAllOrderByFactoryID($context, $result, $start, $configPage['limit'],$factoryID);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderFactoryList', $orderFactoryList);
			
		$smarty->display($templateDir . "/temp1/listOrderFactory.tpl");
	}

}
?>